package org.itboys.mobile.controller.web.product;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.product.SundayProductRecord;
import org.itboys.mobile.service.mysql.web.product.SundayProductRecordService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017/10/25
 * 描述：雄安加盟网_
 */
@Controller
@RequestMapping("/sunday/web/productRecord")
public class SundayProductRecordController extends BaseController{
    @Autowired
    private SundayProductRecordService productRecordService;
    /**
     * 商品记录  主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                         Model model,
                         @RequestParam(value="memberId",required=false)Long memberId){
        model.addAttribute("memberId",memberId);
        return "/sunday/product/record/index/business";
    }

    /**
     * 商品详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            @RequestParam(value="memberId",required=false)Long memberId,
            Model model){

        SundayProductRecord product = productRecordService.findOne(id);
        productRecordService.packageTags(product,true);
        model.addAttribute("product", product);
        model.addAttribute("memberId", memberId);
        return "/sunday/product/record/input/business";
    }
    /**
     * 分页查询商品
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="ids",required=false)List<Long> ids
    ) {
        try {
            PageResult<SundayProductRecord> result=productRecordService.selectProduct(request,ids);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 新增或修改商品
     * @param request
     * @param response
     * @param product
     * @param imageFile
     * @param rollImageFiles
     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @ModelAttribute SundayProductRecord product,
                      @RequestParam(value="imageFile",required=false) MultipartFile imageFile,
                      @RequestParam(value="rollImageFiles",required=false) MultipartFile[] rollImageFiles,
                      @RequestParam(value="memberId",required=false)Long memberId
    ) {
        try {
            productRecordService.save(product, imageFile, rollImageFiles,memberId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/productRecord/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }



}
